<?php

namespace App\Console\Commands\Tools\Camera;

use App\Kuafu\Shop\Model\Shop;
use App\Kuafu\Shop\Model\ShopMonitorDataHourModel;
use App\Kuafu\Shop\Model\ShopMonitorDataModel;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class ExportShopMonitorData extends Command
{
    protected $signature   = 'tools:export-shop-monitor-data';
    protected $description = '导出客流数据';

    public function handle()
    {
        $this->exportAllData();
    }


    private function exportAllData()
    {
        $startTimestamp = Carbon::create(2023, 1, 1)->getTimestamp();
        $endTimestamp   = Carbon::create(2023, 6, 1)->getTimestamp();

        $exportData = [];
        ShopMonitorDataHourModel::query()
                                ->whereIn('type', [
                                    ShopMonitorDataModel::TYPE_IN_SHOP,
                                    ShopMonitorDataModel::TYPE_PASS_SHOP
                                ])
//                                ->whereIn('shopId', [13112, 13410])
                                ->where('dataAt', '>=', $startTimestamp)
                                ->where('dataAt', '<', $endTimestamp)
                                ->chunkById(1000, function ($items) use (&$exportData) {
                                    foreach ($items as $item) {
                                        $key = $item['shopId'] . '_' . date('m', $item['dataAt']);

                                        if (empty($exportData[$key])) {
                                            $exportData[$key] = [
                                                'shopId'  => $item['shopId'],
                                                'month'   => date('m', $item['dataAt']),
                                                'allNum'  => 0,
                                                'inNum'   => 0,
                                                'passNum' => 0
                                            ];
                                        }

                                        if ($item['type'] == ShopMonitorDataModel::TYPE_IN_SHOP) {
                                            $exportData[$key]['inNum'] += $item['count'];
                                        }

                                        if ($item['type'] == ShopMonitorDataModel::TYPE_PASS_SHOP) {
                                            $exportData[$key]['passNum'] += $item['count'];
                                        }

                                        $exportData[$key]['allNum'] += $item['count'];
                                    }
                                });

        $shopIds  = array_unique(array_column($exportData, 'shopId'));
        $shopList = Shop::query()->whereIn('id', $shopIds)->select(['id', 'name', 'shopCode'])->get()->keyBy('id')
                        ->toArray();

        $fileName = '/home/wwwroot/rms-api/allPassenger.csv';
        $row      = '门店名称,类型,01,02,03,04,05' . PHP_EOL;
        file_put_contents($fileName, $row);

        // 指定月份

        foreach ($shopIds as $shopId) {
            $row = vsprintf('%s,%s,%s,%s,%s,%s,%s' . PHP_EOL, [
                $shopList[$shopId]['name'],
                '总客流',
                $exportData[$shopId . '_01']['allNum'] ?? 0,
                $exportData[$shopId . '_02']['allNum'] ?? 0,
                $exportData[$shopId . '_03']['allNum'] ?? 0,
                $exportData[$shopId . '_04']['allNum'] ?? 0,
                $exportData[$shopId . '_05']['allNum'] ?? 0
            ]);
            $row .= vsprintf('%s,%s,%s,%s,%s,%s,%s' . PHP_EOL, [
                $shopList[$shopId]['name'],
                '进店客流',
                $exportData[$shopId . '_01']['inNum'] ?? 0,
                $exportData[$shopId . '_02']['inNum'] ?? 0,
                $exportData[$shopId . '_03']['inNum'] ?? 0,
                $exportData[$shopId . '_04']['inNum'] ?? 0,
                $exportData[$shopId . '_05']['inNum'] ?? 0
            ]);
            $row .= vsprintf('%s,%s,%s,%s,%s,%s,%s' . PHP_EOL, [
                $shopList[$shopId]['name'],
                '经过客流',
                $exportData[$shopId . '_01']['passNum'] ?? 0,
                $exportData[$shopId . '_02']['passNum'] ?? 0,
                $exportData[$shopId . '_03']['passNum'] ?? 0,
                $exportData[$shopId . '_04']['passNum'] ?? 0,
                $exportData[$shopId . '_05']['passNum'] ?? 0
            ]);
            file_put_contents($fileName, $row, 8);
        }

        echo 'done';
    }


}
